package org.nimi317.web_gis.Enum;

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.SneakyThrows;
import org.nd4j.linalg.learning.config.IUpdater;

import java.lang.reflect.Constructor;
import java.util.Map;

/**
 * @author thunderobot
 */
@AllArgsConstructor
@Getter
public enum LstmUpdaterEnum {
    Adam(0, "Adam", "org.nd4j.linalg.learning.config.Adam"),

    RmsProp(1, "RmsProp", "org.nd4j.linalg.learning.config.RmsProp"),

    AdaGrad(2, "AdaGrad", "org.nd4j.linalg.learning.config.AdaGrad"),

    Sgd(3, "Sgd", "org.nd4j.linalg.learning.config.Sgd");

    @EnumValue
    @JsonValue
    @JSONField
    private final int code;

    private final String value;

    private final String updater;

    @SneakyThrows
    public IUpdater resolveUpdater(@NotNull @Min(value = 0) Double learningRate) {
        Constructor<?> constructor = Class.forName(this.updater).getDeclaredConstructor(double.class);
        constructor.setAccessible(true);
        return (IUpdater) constructor.newInstance(learningRate);
    }

    public Map<?,?> getObject() {
        return Map.of("code", this.code, "value", this.value);
    }
}
